//
//  RegistViewController.m
//  group14
//
//  Created by lanou3g on 15/11/11.
//  Copyright © 2015年 吕志伟. All rights reserved.
//

#import "RegistViewController.h"

@interface RegistViewController ()
@property (strong, nonatomic) IBOutlet UITextField *UserNameTextField;
@property (strong, nonatomic) IBOutlet UITextField *PassWordTextfield;
@property (strong, nonatomic) IBOutlet UITextField *ToPassWordTextField;
@property (strong, nonatomic) IBOutlet UITextField *PhoneNumberTextField;
@property (strong, nonatomic) IBOutlet UITextField *EmailTextField;
- (IBAction)Regist:(UIButton *)sender;

@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)Regist:(UIButton *)sender {
    if ([_PassWordTextfield.text isEqual:_ToPassWordTextField.text])
    {
        if ([_UserNameTextField.text isEqual:@""])
        {
            UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"提示:" message:@"请输入用户名:" delegate:nil cancelButtonTitle:@"确定:" otherButtonTitles: nil];
            [av show];
        }
        else if ([_PassWordTextfield.text isEqual:@""])
        {
            UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"提示:" message:@"密码不能为空:" delegate:nil cancelButtonTitle:@"确定:" otherButtonTitles: nil];
            [av show];
        }
        else
        {
            
            AVUser *user = [AVUser user];
            user.username = _UserNameTextField.text;
            user.password =  _PassWordTextfield.text;
            
            user.email = _EmailTextField.text;
            [user setObject:_PhoneNumberTextField.text forKey:@"phone"];
            
            [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    
                    [self.navigationController popViewControllerAnimated:YES];
                    
                } else {
                    
                    UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"提示:" message:@"用户名已存在:" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
                    [av show];
                    
                }
            }];
            
        }
    }
    else
    {
        UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"提示:" message:@"俩次输入的密码不一致:" delegate:nil cancelButtonTitle:@"确认:" otherButtonTitles: nil];
        [av show];
    }

}
@end
